package com.kaitian.demo.demo_boot_starter.controller;

import com.kaitian.demo.demo_boot_starter.annotation.LogAnnotation;
import com.kaitian.demo.demo_boot_starter.dto.DemoDO;
import com.kaitian.demo.demo_boot_starter.service.IDemoService;
import com.kaitian.demo.demo_boot_starter.vo.ResponseVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.Valid;

/**
 * @author ：xietian
 * @version ：V1.0
 * @program ：demo
 * @date ：Created in 2022/4/11 15:58
 * @description ： demo控制器
 */
@ResponseBody
@RestController("/demo")
public class DemoController {

    @Autowired
    private IDemoService demoService;

//    @LogAnnotation(module = "xx", operator = "xxxy")
    @GetMapping("/getBy/{id}")
    public ResponseVO<DemoDO> getById(Integer id) {
        ResponseVO<DemoDO> responseVO = new ResponseVO();
        responseVO.setData(demoService.getById(id));
        return responseVO;
    }

    @LogAnnotation(module = "22xx", operator = "xxx3333y")
    @GetMapping("/getByParam")
    public ResponseVO<DemoDO> getByParam(@RequestParam("id") Integer id) {
        ResponseVO<DemoDO> responseVO = new ResponseVO();
        responseVO.setData(demoService.getByParam(1, "545151852"));
        return responseVO;
    }

    /**
     * 指定状态码 201，创建成功
     * @param dto
     * @return
     */
    @LogAnnotation
    @PostMapping("/save")
    @ResponseStatus(HttpStatus.CREATED)
    public ResponseVO<DemoDO> save(@Valid DemoDO dto) {
        ResponseVO<DemoDO> responseVO = new ResponseVO();
        responseVO.setData(dto);
        return responseVO;
    }

    /**
     * 指定状态码 201，创建成功
     * @param dto
     * @return
     */
    @LogAnnotation
    @PostMapping("/save1")
    public void save1(@Valid DemoDO dto) {
        ResponseVO<DemoDO> responseVO = new ResponseVO();
        responseVO.setData(dto);
    }

    /**
     * 文件
     * @param file
     * @return
     */
    @LogAnnotation
    @PostMapping(value = "/uploadFile", consumes = {MediaType.MULTIPART_FORM_DATA_VALUE})
    public ResponseVO<DemoDO> uploadFile(@RequestParam(value = "file") MultipartFile file) {
        System.out.println(file.getName());
        demoService.getByParamFiles(1, new MultipartFile[] {file});
        ResponseVO<DemoDO> responseVO = new ResponseVO();
        responseVO.setData(demoService.getById(1));
        return responseVO;
    }
}